$(function () {
  let layer = layui.layer

  getUserInfo()

  $('#logout').on('click', function () {
    layer.confirm('退出？你确定？', {
      title: 'heihei'
    }, function (index) {
      location.href = '/login.html'
      localStorage.removeItem('token')
    })
  })
})

function changeLayuiThisToAddarticle() {
  $('#article-list').addClass('layui-this').siblings().removeClass('layui-this')
}

function changeLayuiThisToarticleList() {
  $('#article-add').addClass('layui-this').siblings().removeClass('layui-this')
}

// 获取用户登录信息
function getUserInfo() {
  $.ajax({
    method: "get",
    url: "/my/userinfo",
    success: function (res) {
      if (res.status == 0) {
        let {
          data
        } = res
        console.log(data, 'data');
        // 修改用户头像及昵称
        if (data.user_pic) {
          $('.layui-user .text-avatar').hide()
          $('.layui-user .layui-nav-img').attr('src', data.user_pic)
          $('#user-avatar').hide()
          $('#user-img').attr('src', data.user_pic)
        } else {
          let firstName = data.nickname ? data.nickname[0] : data.username[0]
          $('.layui-user .layui-nav-img').hide()
          $('.layui-user .text-avatar').html(firstName.toUpperCase())
          $('#user-img').hide()
          $('#user-avatar').html(firstName.toUpperCase())
        }
        $('.layui-user .welcome').html('欢迎 ' + data.nickname)
      }
    },

  })
}
